﻿//using System;
//using System.Collections.Generic;
//using System.Windows;
//using System.Windows.Input;

//namespace Dev.Silverlight.Controls
//{    
//    public static class TreeViewWorkarounds
//    {
//        public static System.Windows.Controls.TreeViewItem FindContainer(this System.Windows.Controls.TreeView treeView, Predicate<System.Windows.Controls.TreeViewItem> condition)
//        {
//            return FindContainer(treeView.ItemContainerGenerator, treeView.Items, condition);
//        }

//        private static System.Windows.Controls.TreeViewItem FindContainer(System.Windows.Controls.ItemContainerGenerator parentItemContainerGenerator, System.Windows.Controls.ItemCollection itemCollection, Predicate<System.Windows.Controls.TreeViewItem> condition)
//        {
//            foreach (object curChildItem in itemCollection)
//            {
//                System.Windows.Controls.TreeViewItem containerThatMightMeetTheCondition = (System.Windows.Controls.TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);

//                if (containerThatMightMeetTheCondition == null)
//                    return null;

//                if (condition(containerThatMightMeetTheCondition))
//                    return containerThatMightMeetTheCondition;

//                System.Windows.Controls.TreeViewItem recursionResult = FindContainer(containerThatMightMeetTheCondition.ItemContainerGenerator, containerThatMightMeetTheCondition.Items, condition);
//                if (recursionResult != null)
//                    return recursionResult;
//            }
//            return null;
//        }

//        public static System.Windows.Controls.TreeViewItem ContainerFromItem(this System.Windows.Controls.TreeView treeView, object item)
//        {
//            System.Windows.Controls.TreeViewItem containerThatMightContainItem = (System.Windows.Controls.TreeViewItem)treeView.ItemContainerGenerator.ContainerFromItem(item);
//            if (containerThatMightContainItem != null)
//                return containerThatMightContainItem;
//            else
//                return ContainerFromItem(treeView.ItemContainerGenerator, treeView.Items, item);
//        }

//        private static System.Windows.Controls.TreeViewItem ContainerFromItem(System.Windows.Controls.ItemContainerGenerator parentItemContainerGenerator, System.Windows.Controls.ItemCollection itemCollection, object item)
//        {
//            foreach (object curChildItem in itemCollection)
//            {
//                System.Windows.Controls.TreeViewItem parentContainer = (System.Windows.Controls.TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
//                if (parentContainer == null)
//                    return null;
//                System.Windows.Controls.TreeViewItem containerThatMightContainItem = (System.Windows.Controls.TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);
//                if (containerThatMightContainItem != null)
//                    return containerThatMightContainItem;
//                System.Windows.Controls.TreeViewItem recursionResult = ContainerFromItem(parentContainer.ItemContainerGenerator, parentContainer.Items, item);
//                if (recursionResult != null)
//                    return recursionResult;
//            }
//            return null;
//        }

//        public static object ItemFromContainer(this System.Windows.Controls.TreeView treeView, System.Windows.Controls.TreeViewItem container)
//        {
//            System.Windows.Controls.TreeViewItem itemThatMightBelongToContainer = (System.Windows.Controls.TreeViewItem)treeView.ItemContainerGenerator.ItemFromContainer(container);
//            if (itemThatMightBelongToContainer != null)
//                return itemThatMightBelongToContainer;
//            else
//                return ItemFromContainer(treeView.ItemContainerGenerator, treeView.Items, container);
//        }

//        private static object ItemFromContainer(System.Windows.Controls.ItemContainerGenerator parentItemContainerGenerator, System.Windows.Controls.ItemCollection itemCollection, System.Windows.Controls.TreeViewItem container)
//        {
//            foreach (object curChildItem in itemCollection)
//            {
//                System.Windows.Controls.TreeViewItem parentContainer = (System.Windows.Controls.TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
//                if (parentContainer == null)
//                    return null;
//                System.Windows.Controls.TreeViewItem itemThatMightBelongToContainer = (System.Windows.Controls.TreeViewItem)parentContainer.ItemContainerGenerator.ItemFromContainer(container);
//                if (itemThatMightBelongToContainer != null)
//                    return itemThatMightBelongToContainer;
//                System.Windows.Controls.TreeViewItem recursionResult = ItemFromContainer(parentContainer.ItemContainerGenerator, parentContainer.Items, container) as System.Windows.Controls.TreeViewItem;
//                if (recursionResult != null)
//                    return recursionResult;
//            }
//            return null;
//        }
//    }

//    public class TreeView : System.Windows.Controls.TreeView
//    {
//        protected override bool IsItemItsOwnContainerOverride(object item)
//        {
//            return item is TreeView;
//        }

//        protected override DependencyObject GetContainerForItemOverride()
//        {
//            return new TreeViewItem();
//        }

//        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
//        {
//            TreeViewItem treeViewItemExtended = (TreeViewItem)element;
//            treeViewItemExtended.ParentTreeView = this;

//            base.PrepareContainerForItemOverride(element, item);

//            InvokeContainerPrepared(treeViewItemExtended, item);
//        }

//        public TreeView()
//        {
//            this.DefaultStyleKey = typeof(System.Windows.Controls.TreeView);
//        }

//        public IEnumerable<System.Windows.Controls.TreeViewItem> Containers
//        {
//            get
//            {
//                return GetTreeViewItems(this.ItemContainerGenerator, this.Items);
//            }
//        }

//        private static IEnumerable<System.Windows.Controls.TreeViewItem> GetTreeViewItems(System.Windows.Controls.ItemContainerGenerator parentItemContainerGenerator, System.Windows.Controls.ItemCollection itemCollection)
//        {
//            foreach (object curChildItem in itemCollection)
//            {
//                System.Windows.Controls.TreeViewItem container = (System.Windows.Controls.TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);

//                if (container != null)
//                    yield return container;

//                foreach (var treeViewItem in GetTreeViewItems(container.ItemContainerGenerator, container.Items))
//                    yield return treeViewItem;
//            }
//        }

//        public event RoutedEventHandler ContainerExpanded;
//        internal void InvokeContainerExpanded(object sender, RoutedEventArgs e)
//        {
//            RoutedEventHandler expanded = ContainerExpanded;
//            if (expanded != null) expanded(sender, e);
//        }

//        public event RoutedEventHandler ContainerCollapsed;
//        internal void InvokeContainerCollapsed(object sender, RoutedEventArgs e)
//        {
//            RoutedEventHandler collapsed = ContainerCollapsed;
//            if (collapsed != null) collapsed(sender, e);
//        }

//        public event MouseButtonEventHandler ContainerMouseLeftButtonDownEventHandler;
//        internal void InvokeContainerMouseLeftButtonButtonEventHandler(object sender, MouseButtonEventArgs e)
//        {
//            MouseButtonEventHandler handler = ContainerMouseLeftButtonDownEventHandler;
//            if (handler != null) handler(sender, e);
//        }

//        public event EventHandler<ContainerPreparedEventArgs> ContainerPrepared;
//        internal void InvokeContainerPrepared(TreeViewItem sender, object item)
//        {
//            EventHandler<ContainerPreparedEventArgs> prepared = ContainerPrepared;
//            if (prepared != null) prepared(sender, new ContainerPreparedEventArgs(sender, item));

//            if (itemsToDelayExpand.Contains(item))
//            {
//                sender.IsExpanded = true;
//                itemsToDelayExpand.Remove(item);
//            }
//        }

//        private List<object> itemsToDelayExpand = new List<object>();
//        public void ExpandDelayItems(params object[] ItemsCorrespondingToTreeViewItemsToExpand)
//        {
//            itemsToDelayExpand.AddRange(ItemsCorrespondingToTreeViewItemsToExpand);

//            foreach (object itemtoTryAndExpand in ItemsCorrespondingToTreeViewItemsToExpand)
//            {
//                System.Windows.Controls.TreeViewItem treeViewItem = this.ContainerFromItem(itemtoTryAndExpand);
//                if (treeViewItem != null)
//                {
//                    treeViewItem.IsExpanded = true;
//                    itemsToDelayExpand.Remove(itemtoTryAndExpand);
//                }
//            }
//        }

//        private object SelectedItemDelayed = null;
//        public void SetSelectedItem(object SelectedItem, params object[] SelectedItemParents)
//        {
//            ContainerPrepared += new EventHandler<ContainerPreparedEventArgs>(ContainerPrepared_LookForSelectedItem);
//            SelectedItemDelayed = SelectedItem;
//            ExpandDelayItems(SelectedItemParents);
//        }

//        private void ContainerPrepared_LookForSelectedItem(object sender, ContainerPreparedEventArgs e)
//        {
//            if (e.Item == SelectedItemDelayed)
//            {
//                e.Container.IsSelected = true;
//                SelectedItemDelayed = null;
//                ContainerPrepared -= new EventHandler<ContainerPreparedEventArgs>(ContainerPrepared_LookForSelectedItem);
//            }
//        }

//    }

//    public class ContainerPreparedEventArgs : EventArgs
//    {
//        public ContainerPreparedEventArgs(TreeViewItem container, object item)
//        {
//            Container = container;
//            Item = item;
//        }

//        public TreeViewItem Container { get; set; }
//        public object Item { get; set; }
//    }

//    public class TreeViewItem : System.Windows.Controls.TreeViewItem
//    {
//        public TreeViewItem()
//        {
//            this.Expanded += new RoutedEventHandler(TreeViewItemExtended_Expanded);
//            this.Collapsed += new RoutedEventHandler(TreeViewItemExtended_Collapsed);
//        }

//        void TreeViewItemExtended_Collapsed(object sender, RoutedEventArgs e)
//        {
//            ParentTreeView.InvokeContainerCollapsed(sender, e);
//        }

//        void TreeViewItemExtended_Expanded(object sender, RoutedEventArgs e)
//        {
//            ParentTreeView.InvokeContainerExpanded(sender, e);
//        }

//        protected override bool IsItemItsOwnContainerOverride(object item)
//        {
//            return item is TreeView;
//        }

//        protected override DependencyObject GetContainerForItemOverride()
//        {
//            return new TreeViewItem();
//        }

//        public TreeView ParentTreeView { internal set; get; }
//        public TreeViewItem ParentTreeViewItem { internal set; get; }

//        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
//        {
//            TreeViewItem treeViewItemExtended = (TreeViewItem)element;
//            treeViewItemExtended.ParentTreeView = this.ParentTreeView;
//            treeViewItemExtended.ParentTreeViewItem = this;

//            base.PrepareContainerForItemOverride(element, item);

//            ParentTreeView.InvokeContainerPrepared(treeViewItemExtended, item);
//        }
//    }

//}
